package com.hxkcloud.patient.activitys;

import android.content.Intent;
import android.os.Message;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.adapter.PictureConsultAdapter;
import com.hxkcloud.patient.beans.ConsulteBean;
import com.hxkcloud.patient.beans.ConsulteBeanList;
import com.hxkcloud.patient.beans.UchatBean;
import com.hxkcloud.patient.beans.UchatListBean;
import com.hxkcloud.patient.request.MessageCode;
import com.hxkcloud.patient.request.YiJiaHuNetWorkUtils;

import java.util.ArrayList;
import java.util.List;

public class PictureConsultActivity extends BaseActivity implements View.OnClickListener{
    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private TextView tv_complete;

    //提问按钮
    private TextView tv_ask;
    private TextView tv_group;
    private TextView tv_num;

    //图文列表
    private ListView listView_Pic;
    //private PictureConsultAdapter adapter;
    private PictureConsultAdapter pictureConsultAdapter;
  //  private List<MsgBean> list_data;
    private int startWz = 0;
    private ConsulteBeanList consulteBeanList;
    private List<ConsulteBean> consult_list;
    private UchatListBean uchatList;
    private List<UchatBean> uchat;
    private int counts = 0;
    private String ID = "";
    private String isAchieve="";
    private LinearLayout ll_ask;
    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what){
            case MessageCode.NETWORKCODE_USERCOUNT_SUCCESS:
                if (msg.obj!=null){
                    dismisssLoading();
                    consulteBeanList = (ConsulteBeanList) msg.obj;
                    consult_list.clear();
                    consult_list.addAll(consulteBeanList.getJobGroup());
                    tv_group.setText(consult_list.get(0).getGroup());
                    counts = consult_list.get(0).getCount();
                    tv_num.setText(counts+"/"+consult_list.get(0).getNumber());
                    startWz = consult_list.get(0).getStartWz();
                    if (startWz==0){
                        tv_ask.setBackgroundResource(R.drawable.consult_text_backgroud_red);
                    }else if (startWz == 1){
                        tv_ask.setBackgroundResource(R.drawable.consult_text_backgroud_gray);
                    }
                 }

                break;
            case MessageCode.NETWORKCODE_USERCOUNT_FAILURE:
                if ((msg.obj != null) && (!TextUtils.isEmpty((String) msg.obj))) {
                    toastMsg((String) msg.obj);
                } else {
                    toastMsg(R.string.datagetfail);
                }
                break;
            case MessageCode.NETWORKCODE_USERANDDOCTORCHAT_SUCCESS:
                if (msg.obj!=null){
                    uchat.clear();
                    uchatList = (UchatListBean) msg.obj;
                    uchat.addAll(uchatList.getUchat());
                   // Log.i("----uchat----->","uchat:"+uchat.size()+uchat.toString());

                }
                pictureConsultAdapter.notifyDataSetChanged();
                break;
            case MessageCode.NETWORKCODE_USERANDDOCTORCHAT_FAILURE:
                if ((msg.obj != null) && (!TextUtils.isEmpty((String) msg.obj))) {
                    toastMsg((String) msg.obj);
                } else {
                    toastMsg(R.string.datagetfail);
//                    toastMsg("聊天信息请求失败");
                }
                break;
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture_consult);
        consult_list = new ArrayList<>();
        uchat = new ArrayList<>();
        if (getIntent().hasExtra("id")){
            ID = getIntent().getIntExtra("id",0)+"";
        }

        initView();
        initData();
        initListener();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        tv_complete = (TextView) findViewById(R.id.tv_title_func);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText("提问");

        ll_ask = (LinearLayout) findViewById(R.id.consult_linear_bottom);
        tv_ask = (TextView) findViewById(R.id.picture_consult_ask);
        tv_group = (TextView) findViewById(R.id.tv_pictureconsult_group);
        tv_num = (TextView) findViewById(R.id.consultant_number);
        listView_Pic = (ListView) findViewById(R.id.picture_consult_listview);
    }

    @Override
    protected void initListener() {
        iv_back.setOnClickListener(this);
        tv_ask.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        if (getIntent().hasExtra("isAchieve")){
            isAchieve = getIntent().getStringExtra("isAchieve");
            ll_ask.setVisibility(View.GONE);
        }else {
            ll_ask.setVisibility(View.VISIBLE);
        }

        pictureConsultAdapter = new PictureConsultAdapter(mContext,uchat);
        listView_Pic.setAdapter(pictureConsultAdapter);
        loading();
        YiJiaHuNetWorkUtils.UserCount(loginInfo.getUserId(),handler);
        YiJiaHuNetWorkUtils.UserAndDoctorChat(loginInfo.getUserId(),ID,"User",handler);
    }

    @Override
    public void onResume() {
        super.onResume();
//        loading();
//        YiJiaHuNetWorkUtils.UserCount(loginInfo.getUserId(),handler);
//        YiJiaHuNetWorkUtils.UserAndDoctorChat(loginInfo.getUserId(),ID,handler);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        loading();
        YiJiaHuNetWorkUtils.UserCount(loginInfo.getUserId(),handler);
        YiJiaHuNetWorkUtils.UserAndDoctorChat(loginInfo.getUserId(),ID,"User",handler);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.picture_consult_ask:
                if (counts>0){
                    if (startWz ==0 ){

                        Intent intent  = new Intent(mContext,AskQuestionActivity.class);
                        startActivityForResult(intent,1);
                    }else {
                        toastMsg("医生未回复,无法提问");
                    }
                }else {
                    toastMsg("未购买咨询服务,请先购买");
                }
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode==1){
            if (resultCode ==2){
                loading();
                YiJiaHuNetWorkUtils.UserCount(loginInfo.getUserId(),handler);
                YiJiaHuNetWorkUtils.UserAndDoctorChat(loginInfo.getUserId(),ID,"User",handler);
            }
        }
    }
}
